package com.dc.base.util.wechat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 微信创建自定义菜单
 * @author Shen
 * @date 2018年4月10日 下午2:39:16
 */
public class WechatMenuUtil {
	
	private static String createMuneUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";   
    private static String delMuneUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";  
   
    public static String getWechatsToken() throws IOException{  
        HttpURLConnection httpConn = null;  
        String requesturl = "https://api.weixin.qq.com/cgi-bin/token";  
        String appid = WechatUtil.appID;  
        String appsecret = WechatUtil.appsecret;  
        requesturl += "?grant_type=client_credential&appid="+appid;  
        requesturl += "&secret="+appsecret;  
        URL url = new URL(requesturl);  
        httpConn = (HttpURLConnection) url.openConnection();  
        httpConn.setDoOutput(true);  
        httpConn.setDoInput(true);  
        httpConn.setUseCaches(false);  
        httpConn.setRequestProperty("Content-Type", "GET");  
        httpConn.connect();  
        OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());  
        out.flush();  
        out.close();  
        //接收返回  
        BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));  
        String line;  
        StringBuffer sb = new StringBuffer();  
        while ((line = bf.readLine()) != null) {  
            sb.append(line);  
        }  
        bf.close();  
        httpConn.disconnect();  
        String result = new String(sb);  
        System.out.println("返回输出:"+result);  
        return result;  
    }  
    public static String createMune() throws Exception{  
        JSONObject json = new JSONObject();  
        JSONArray ja = new JSONArray(); 
        
          
        JSONObject js1 = new JSONObject();  
        js1.put("name", "最多跑一次");  
        
        JSONArray jsub1 = new JSONArray();  
        JSONObject jsonsub11 = new JSONObject();  
        jsonsub11.put("type", "view");  
        jsonsub11.put("name", "违法查询");  
        jsonsub11.put("url", "http://jix.122.gov.cn/views/inquiry.html?q=j#info-wf");  
        jsub1.add(jsonsub11);  
          
        JSONObject jsonsub12 = new JSONObject();  
        jsonsub12.put("type", "view");  
        jsonsub12.put("name", "记分查询");  
        jsonsub12.put("url", "http://www.zjsgat.gov.cn:8080/was/phone/carcredit/carCreditQuery.jsp");  
        jsub1.add(jsonsub12);  
          
        JSONObject jsonsub13 = new JSONObject();  
        jsonsub13.put("type", "view");  
        jsonsub13.put("name", "业务指南");  
        //jsonsub13.put("url", "http://syrq.eu235.cn/hyjt/wechat/businessGuide.jsp");  
        jsonsub13.put("url", "http://jix.122.gov.cn/views/help.html");  
        jsub1.add(jsonsub13);  
          
        js1.put("sub_button", jsub1);  
         
          
        JSONObject js2 = new JSONObject();  
        js2.put("name", "微服务");  
        JSONArray jsub2 = new JSONArray();  
        JSONObject jsonsub2 = new JSONObject();  
        jsonsub2.put("type", "view");  
        jsonsub2.put("name", "违法举报");  
        jsonsub2.put("url", "http://weixin.jtpk668.com/report/info");  
        jsub2.add(jsonsub2);  
          
        JSONObject jsonsub3 = new JSONObject();  
        jsonsub3.put("type", "view");  
        jsonsub3.put("name", "出行服务");  
        jsonsub3.put("url", "http://61.153.228.142/hyjt/wechat/travelService.jsp");  
        jsub2.add(jsonsub3);  
          
        js2.put("sub_button", jsub2);
        
        JSONObject js3 = new JSONObject();  
        js3.put("name", "微学堂");  
        JSONArray jsub3 = new JSONArray();  
        JSONObject jsonsub31 = new JSONObject();  
        jsonsub31.put("type", "view");  
        jsonsub31.put("name", "交通专题视频");  
        jsonsub31.put("url", "http://v.qq.com/vplus/6055ca75a371509cd198c58fb86eb4dc/foldervideos/v2a0000012jbyvo");  
        jsub3.add(jsonsub31);  
          
        JSONObject jsonsub32 = new JSONObject();  
        jsonsub32.put("type", "view");  
        jsonsub32.put("name", "法律法规考试");  
        jsonsub32.put("url", "http://61.153.228.142/hyjt/wechat/ready.jsp");  
        jsub3.add(jsonsub32);  
  
          
        js3.put("sub_button", jsub3); 
          
          
        ja.add(js1); 
        ja.add(js2);  
        ja.add(js3);  
        json.put("button", ja); 
        System.out.println(json.toString());
        HttpURLConnection httpConn = null;  
        String pathUrl = createMuneUrl+WechatUtil.getBaseAccessToken();  
        URL url = new URL(pathUrl);  
        httpConn = (HttpURLConnection) url.openConnection();  
        httpConn.setDoOutput(true);  
        httpConn.setDoInput(true);  
        httpConn.setUseCaches(false);  
        httpConn.setRequestProperty("Content-Type", "GET");  
        httpConn.connect();  
        OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());  
        out.write(new String(json.toString().getBytes("UTF-8")));  
        out.flush();  
        out.close();  
        //接收返回  
        BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));  
        String line;  
        StringBuffer sb = new StringBuffer();  
        while ((line = bf.readLine()) != null) {  
            sb.append(line);  
        }  
        bf.close();  
        httpConn.disconnect();  
        String result = new String(sb);  
        System.out.println("返回输出:"+result);  
        return result;  
    }  
      
    public static void delMuneUrl() throws IOException{  
        HttpURLConnection httpConn = null;  
        String pathUrl = delMuneUrl+WechatUtil.getBaseAccessToken();  
        URL url = new URL(pathUrl);  
        httpConn = (HttpURLConnection) url.openConnection();  
        httpConn.setDoOutput(true);  
        httpConn.setDoInput(true);  
        httpConn.setUseCaches(false);  
        httpConn.setRequestProperty("Content-Type", "GET");  
        httpConn.connect();  
        OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());  
        out.flush();  
        out.close();  
        //接收返回  
        BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));  
        String line;  
        StringBuffer sb = new StringBuffer();
        while ((line = bf.readLine()) != null) {  
            sb.append(line);  
        }  
        bf.close();  
        httpConn.disconnect();  
        String result = new String(sb);  
        System.out.println("返回输出:"+result);  
    }  
    public static void main(String[] args) throws Exception{   
    	//微信创建自定义菜单，可以满足5各字
        WechatMenuUtil.createMune();  
    } 
}

